package liangtWorkSpace.Observer.base;

import liangtWorkSpace.Observer.HeadFiestObsver.display.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * @ClassName CurrentConditionsDisplay
 * @Description : 调用JDK本地的观察者实现观察者模式
 * @Author : LiangT
 * @Date : 2020/6/19 11:26
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {
    private Observable observable;
    private float humidity;
    private float temperature;


    public CurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    /**
     * 赋值
     * @param o
     * @param arg
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData){
            WeatherData weatherData = (WeatherData) o;
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            disPlay();
        }
    }

    /**
     * 展示
     */
    @Override
    public void disPlay() {
        System.out.println("Current conditions: " + temperature
                + "F degrees and " + humidity + "% humidity");
    }
}
